package com.dianping.cat.configuration.web.url.transform;

import com.dianping.cat.configuration.web.url.IVisitor;
import com.dianping.cat.configuration.web.url.entity.Code;
import com.dianping.cat.configuration.web.url.entity.PatternItem;
import com.dianping.cat.configuration.web.url.entity.UrlPattern;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;



public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(UrlPattern urlPattern) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(urlPattern, out);
    return out.toByteArray();
  }


  public static void build(UrlPattern urlPattern, OutputStream out) { urlPattern.accept(new DefaultNativeBuilder(out)); }



  public void visitCode(Code code) {
/*  41 */     if (code.getId() != null) {
/*  42 */       writeTag(1, 1);
/*  43 */       writeInt(code.getId().intValue());
    }

/*  46 */     if (code.getName() != null) {
/*  47 */       writeTag(2, 1);
/*  48 */       writeString(code.getName());
    }

/*  51 */     if (code.getStatus() != null) {
/*  52 */       writeTag(3, 1);
/*  53 */       writeInt(code.getStatus().intValue());
    }

/*  56 */     writeTag(63, 3);
  }


  public void visitPatternItem(PatternItem patternItem) {
/*  61 */     if (patternItem.getGroup() != null) {
/*  62 */       writeTag(1, 1);
/*  63 */       writeString(patternItem.getGroup());
    }

/*  66 */     if (patternItem.getName() != null) {
/*  67 */       writeTag(2, 1);
/*  68 */       writeString(patternItem.getName());
    }

/*  71 */     if (patternItem.getPattern() != null) {
/*  72 */       writeTag(3, 1);
/*  73 */       writeString(patternItem.getPattern());
    }

/*  76 */     if (patternItem.getDomain() != null) {
/*  77 */       writeTag(4, 1);
      writeString(patternItem.getDomain());
    }

/*  81 */     writeTag(5, 0);
/*  82 */     writeInt(patternItem.getId());

/*  84 */     writeTag(63, 3);
  }


  public void visitUrlPattern(UrlPattern urlPattern) {
/*  89 */     writeTag(63, 0);

/*  91 */     if (!urlPattern.getPatternItems().isEmpty()) {
/*  92 */       writeTag(33, 2);
/*  93 */       writeInt(urlPattern.getPatternItems().size());

/*  95 */       for (PatternItem patternItem : urlPattern.getPatternItems().values()) {
/*  96 */         patternItem.accept(this.m_visitor);
      }
    }

/* 100 */     if (!urlPattern.getCodes().isEmpty()) {
/* 101 */       writeTag(34, 2);
/* 102 */       writeInt(urlPattern.getCodes().size());

/* 104 */       for (Code code : urlPattern.getCodes().values()) {
/* 105 */         code.accept(this.m_visitor);
      }
    }

/* 109 */     writeTag(63, 3);
  }

  private void writeInt(int value) {
    try {
/* 114 */       writeVarint(value & 0xFFFFFFFFL);
/* 115 */     } catch (IOException e) {
/* 116 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 122 */       this.m_out.writeUTF(value);
/* 123 */     } catch (IOException e) {
/* 124 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 130 */       this.m_out.writeByte((field << 2) + type);
/* 131 */     } catch (IOException e) {
/* 132 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 138 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 139 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 142 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 143 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-core-3.0.0.jar!/com/dianping/cat/configuration/web/url/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */